//
//  ProductInfoModel.m
//  DA_V
//
//  Created by 中亿世联前台 on 15/5/14.
//  Copyright (c) 2015年 zhouhai. All rights reserved.
//

#import "ProductInfoModel.h"
#import "AFNetworking.h"
#import "ProductListModel.h"
#import "EncryptionData.h"
#import "InterFace.h"

@implementation ProductInfoModel

- (void)setValue:(id)value forUndefinedKey:(NSString *)key{}
#pragma -mark 向服务器请求获取商品详情信息
- (void)getProductInfo:(NSMutableArray *)array parameter:(NSDictionary *)parameter{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects: @"text/html", nil];
    
    
//    NSMutableArray *parameterArray = [[NSMutableArray alloc]init];
//    [parameterArray addObject:parameter];
    NSData *jsonData = [NSJSONSerialization dataWithJSONObject:parameter options:NSJSONWritingPrettyPrinted error:nil];
    
    
    NSString *jsonString = [jsonData base64EncodedStringWithOptions:0];
    

    
    NSString *str = [self encodeString:jsonString key:PasswordKey];
    
    NSDictionary *paramer = @{@"sign":str};
    
    [manager POST:ProductInfoInterface parameters:paramer success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        
        
        
        NSString *str = responseObject[@"sign"];
        
        
        EncryptionData *data = [[EncryptionData alloc]init];
        NSString *datastr = [data decryption:str :PasswordKey];
        
        NSData* dataFromString = [datastr dataUsingEncoding:NSUTF8StringEncoding];
        
        NSDictionary *dataDic = [NSJSONSerialization  JSONObjectWithData:dataFromString options:NSJSONReadingMutableContainers error:nil];
        
        //返回成功
        if ( [[dataDic objectForKey:@"status"] isEqualToString:@"success"]) {
            
            NSLog(@"商品详情返回结果 ＝＝＝＝ %@",dataDic);
            NSDictionary *myDataDic =dataDic[@"data"];

            
            [self setValuesForKeysWithDictionary:myDataDic];
            
            NSArray *ThumbPathList = myDataDic[@"ThumbPathList"];
            for (NSString *url in ThumbPathList) {
                [array addObject:url];
            }
            
            [self.productInfoModelDelegate getProDuctInfoInterfaceEnd];
        } else if ( [[dataDic objectForKey:@"status"] isEqualToString:@"failed"]) {
//            [self.productInfoModelDelegate getProDuctInfoInterfaceEnd];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:[dataDic objectForKey:@"reason"] delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
            [self.productInfoModelDelegate getrequesterror:[dataDic objectForKey:@"reason"]];
        } else{
//            [self.productInfoModelDelegate getProDuctInfoInterfaceEnd];
//            UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"服务器异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//            [alert show];
            [self.productInfoModelDelegate getrequesterror:@"服务器异常,无法加载数据"];
        }
        
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        [self.productInfoModelDelegate getProDuctInfoInterfaceEnd];
//        NSLog(@"========== %@", [error description]);
//        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"网络异常,无法加载数据" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
//        [alert show];
        [self.productInfoModelDelegate requesterror];
        
    }];
    
}
#pragma mark - 加密
-(NSString *)encodeString:(NSString *)data key:(NSString *)key{
    //    NSData* sampleData = [data dataUsingEncoding:NSUTF8StringEncoding];
    //
    //    NSString * base64String = [sampleData base64EncodedStringWithOptions:0];
    NSLog(@"%@",data);
    NSString *result=[[NSMutableString alloc] init];
    NSInteger keyLength = [key length];
    int position = 0;
    int keyChar = 0;
    int strChar = 0;
    for (int i = 0; i < [data length]; i++) {
        position = i % keyLength;
        strChar = [data characterAtIndex:i];
        keyChar =[key characterAtIndex:position];
        int  ch = strChar ^ keyChar;
        char ca = (char) ch;
        result=[result stringByAppendingString:[NSString stringWithFormat:@"%C",ca]];
    }
    
    NSData* sampleDat1a = [result dataUsingEncoding:NSUTF8StringEncoding];
    
    NSString * base64String1 = [sampleDat1a base64EncodedStringWithOptions:0];
    
    return base64String1;
}
@end
